import os
import sys
import kyMD5
import dirdb
def walk_dir(dirpath,output):
    walkdir = os.walk(dirpath)
    f = open(output,"w")
    for ftube in walkdir:
        for file in ftube[2]:
            s = ftube[0] + "/"+file
            print s
            f.write(file + "\r\n")
    f.close()
def make_destdb(dirpath):
    print "counting destation files......"
    d = dirdb.dirdb()
    d.createDb()
    walkdir = os.walk(dirpath)
    for ftube in walkdir:
        for file in ftube[2]:
            fullname = ftube[0] + "/"+file
            fileMd5 = kyMD5.md5sum(fullname)
            d.insertData(file,fullname,fileMd5) 
    print "ok"
def query_repeat(dirpath):
    print "start ......"
    d = dirdb.dirdb()
    d.createDb()
    walkdir = os.walk(dirpath)
    for ftube in walkdir:
        for file in ftube[2]:
            fullname = ftube[0] + "/"+file
            fileMd5 = kyMD5.md5sum(fullname)
            d.queryMD5(fileMd5) 
            if len(d.c.fetchall()) == 0:
                print fullname + " not repeat!!"
    print "ok"


print "argv :",sys.argv
print len(sys.argv)
if len(sys.argv) > 1:
    source_dir = sys.argv[1]
    dest_dir = sys.argv[2]
    #walk_dir(source_dir,"source")
    #walk_dir(dest_dir,"dest")
    make_destdb(dest_dir)
    query_repeat(source_dir)
